Skip to content

Conversation

sejas
Copy link

@sejas sejas commented Oct 3, 2025

This PR applies the wp_sqlite_default_db_name filter in all cases, even if DB_NAME was defined, so we can override the database name, and fix the behaviour in remote servers where DB_NAME is already defined.

Testing instructions:

Warning

These steps are not currently working, but they represent the desired behavior.

  • Break your database connection:
    • Go to your wp-config.php
    • Copy the value of your DB_NAME
    • Change it to any value to break your database connection
    • Access the homepage of your site and observe the error: Error establishing a database connection
  • Fix your database connection using the filter:
    • Create a new mu-plugin with the code below these instructions.
    • Access your homepage
    • Observe the site loads correctly
<?php

add_filter( 'wp_sqlite_default_db_name', function( $db_name ) {
	return 'database_name_here'; // Or any other value that you copied from your wp-config.php
} );

@sejas
Copy link
Author

sejas commented Oct 3, 2025

@JanJakes, I've created this PR trying to override the DB_NAME when we don't have control over that variable, however the filter is not working. Do you know how should I define the filter?
If the wp-includes/sqlite/db.php file loads before any plugin, why do we have a filter in the first place?

@sejas
Copy link
Author

sejas commented Oct 3, 2025

@sejas sejas closed this Oct 3, 2025
@JanJakes
Copy link
Member

JanJakes commented Oct 6, 2025

@sejas Thanks for looking into this!

Closing in favor of #261

Yeah, good call. This filter is not meant to be used that way and will likely be removed.

@JanJakes JanJakes mentioned this pull request Oct 14, 2025
JanJakes added a commit that referenced this pull request Oct 15, 2025
This PR replaces
#262. It is
a simpler solution to the same problem:

When a table reference targets an information schema table, we replace
it with a subquery, injecting the configured database name dynamically:

```sql
-- The following query:
SELECT *, t.*, t.table_schema FROM information_schema.tables t

-- Will be translated to:
SELECT *, t.*, t.table_schema FROM (
  SELECT
    `TABLE_CATALOG`,
    IIF(`TABLE_SCHEMA` = 'information_schema', `TABLE_SCHEMA`, 'database_name') AS `TABLE_SCHEMA`,
    `TABLE_NAME`,
    ...
  FROM _wp_sqlite_mysql_information_schema_tables AS tables
) t
```
The same logic will be applied to table references in JOIN clauses as
well.

---

With the new SQLite driver, we keep running into issues with missing and
incorrect database name values
(#197,
#203,
#226,
#260,
#261, and
other issues).

Thinking through this further, I came to the conclusion that to provide
maximum flexibility and portability, it would be best to provide an API
in the shape of:

_"Mount `my-sqlite-file.sqlite` as `my-db-name`."_

Even if we consider adding multi-database support one day, it would
still be great to allow mounting additional SQLite files under dynamic
database names.

I don't see any downsides to doing this, except maybe that we'll have to
choose some database name in some scenarios, such as in database admin
tools. However, in these cases we can use a reasonable default or the
file name.

This is a WIP pull request demonstrating the simplest approach. For it
to be merged, I only need to resolve how the existing database name
values should be treated.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants